package khleuven.mondzorg.app;

import java.util.List;

import khleuven.mondzorg.Logger;
import khleuven.mondzorg.R;
import khleuven.mondzorg.domain.EntryType;
import khleuven.mondzorg.domain.Modules;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ModulesActivity extends ActivityWithSettingsAndHelp {
	
	public static final String ORIGIN_KEY = "origin";
	public static final String MODULE_NAME = "module";
	private EntryType origin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_modules);
		fetchIntentExtras();
		
		ListView  myListView = (ListView)findViewById(R.id.moduleList);
		final List<String> modulelijst = Modules.getInstance().getAllModulesAsStringList();
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.modulelist_item, modulelijst);
		myListView.setAdapter(adapter);
		
		myListView.setOnItemClickListener(new OnItemClickListener() {
			  @Override
			  public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
				  String clicked = modulelijst.get(pos);
				  Logger.logApp("geklikt op: "+clicked);
				  Logger.logApp("ORIGIN_KEY: "+ORIGIN_KEY);
				  if(origin == EntryType.LEARN){
					  Logger.logApp("go to learn");
					  Intent intent = new Intent(ModulesActivity.this, LearnActivity.class);
		      		  intent.putExtra(ModulesActivity.MODULE_NAME, clicked);
		      		  startActivity(intent);

				  } else if(origin == EntryType.TEST){
					Intent intent = new Intent(ModulesActivity.this, TestActivity.class);
		      		intent.putExtra(ModulesActivity.MODULE_NAME, clicked);
		      		startActivity(intent);			
					  Logger.logApp("go to test");
			    } else{
			    	Logger.logApp("go to nothing");
			    }
			  }
			});

	}

	private void fetchIntentExtras(){
		Logger.logApp("aangekomen in fetchIntentExtras");
		Bundle b = getIntent().getExtras();
		if(b!=null){
			Logger.logApp("aangekomen in fetchIntentExtras - bundle niet null");
			origin = EntryType.valueOf(b.getString(ORIGIN_KEY));
			Logger.logApp("aangekomen in fetchIntentExtras - bundle niet null origin:"+origin);
		}
	}
}
